package com.ruoyi.web.controller.myController;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.LoginServiceImpl;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.Myuser;
import com.ruoyi.system.domain.vo.LoginDTO;
import com.ruoyi.system.domain.vo.Results;
import com.ruoyi.system.service.IMyuserService;
import io.jsonwebtoken.Claims;
import org.apache.el.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@RestController
public class LoginController {
    @Autowired
    LoginServiceImpl loginService;
    @Autowired
    private IMyuserService myuserService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private TokenService tokenService;
    @PostMapping("/api/login")
    @CrossOrigin
    public Results login(@RequestBody LoginDTO loginDTO){

       // System.out.println(loginDTO.getLoginName()+"  "+loginDTO.getPassword());
        return loginService.login(loginDTO);
    }
    @PostMapping("/api/selectByid/{id}")
    @CrossOrigin
    public Results selectUser(@PathVariable("id") Integer id){
        Myuser user = myuserService.selectMyuserByUserid(id.longValue());
        return new Results(200,"",user);
    }
        @GetMapping("/api/selectByUserId/{userid}")
    @CrossOrigin
    public Results selectByUser(@PathVariable("userid") Integer userid){
        Myuser user = myuserService.selectMyuserByUserid(userid.longValue());
        return new Results(200,"",user);
    }
    @GetMapping("/api/refresh/{token}")
    public Results selectToken(@PathVariable("token") String token) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Long loginTime = tokenService.getLoginTime(token);
        if(loginTime==null){
            return new Results(400,"",null);
        }
        return new Results(200,"",loginTime);
    }
}
